home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 3d_lib.zip / DF.C < prev    next >
C/C++ Source or Header  |  1993-05-09  |  3KB  |  81 lines

  1. /* Display face on screen
  2.  
  3.    Copyright (c) 1988 by Gus O'Donnell
  4.  
  5.    Revision history:
  6.  
  7.    Version 1.00         February 29, 1988       As released.
  8.  
  9.    Version 1.01         March 20, 1988          Created libraries for all
  10.                                                 memory models
  11.  
  12. */
  13. #include "3d.h"
  14. #include <float.h>
  15. #include <graphics.h>
  16. #include <math.h>
  17. #include <stdio.h>
  18.  
  19. void    disp_face (VECTOR lsource, int color, FACE *this_face, MATRIX xfrm_mat)
  20.  
  21. /* Display a face on the screen using the current transform matrix.
  22.  
  23. The shade of the face is determined by the angle between the light source,
  24. lsource, and the normal to the face.  The displayed shade is proportional
  25. to the cosine of this angle.  A temporary face is constructed from the
  26. untransformed face and the transform matrix.  The normal is calculated.
  27. If the z component of the normal is positive, the face is not displayed
  28. (the assumption is that the face is on the far side of the object).
  29.  
  30. This function will display a polygon with a maximum of 10 corners
  31. */
  32.  
  33. {
  34.     VECTOR tnorm;               /* Normal to the transformed face */
  35.     CORNER *chandle;            /* Pointer for traversing the corner
  36.                                    list */
  37.     VERTEX tvec;                /* Temporary storage for vec_mul result */
  38.     double angle;               /* Cosine of angle between tnorm and
  39.                                    lsource */
  40.     int vertices [22];          /* Corner list */
  41.     int ncorners;               /* Number of corners in the face */
  42.     char pat [8];               /* Shaded fill pattern */
  43.     int bcount,ccount,incr;     /* Counters to create fill pattern */
  44.     int err;
  45.  
  46.     err = normal (this_face,tnorm);
  47.     if (err != 0) return;              /* Return if error */
  48.     if ((tnorm [2]) <= 0.0) return;       /* Don't display if facing away */
  49.     ncorners = 0;
  50.     chandle = this_face -> first -> next;
  51.     while (((chandle -> next) != NULL) && (ncorners < 10))
  52.     {
  53.         vec_mul (chandle -> this,xfrm_mat,&tvec);
  54.         vertices [ncorners * 2] = tvec.coord [0];
  55.         vertices [ncorners * 2 + 1] = tvec.coord [1];
  56.         chandle = chandle -> next;
  57.         ncorners++;
  58.     }
  59.     vertices [ncorners * 2] = vertices [0];
  60.     vertices [ncorners * 2 +1] = vertices [1];
  61.     angle = 100 * dot_prod (tnorm,lsource);
  62.     incr = 0;
  63.     for (ccount = 0; ccount < 8; ccount++)
  64.     {
  65.         pat [ccount] = 0;
  66.         for (bcount = 0; bcount < 8; bcount++)
  67.         {
  68.             pat [ccount] = pat [ccount] * 2;
  69.             incr = incr + angle;
  70.             if (incr >= 100)
  71.             {
  72.                 pat [ccount]++;
  73.                 incr = incr - 100;
  74.             }
  75.         }
  76.     }
  77.     setfillpattern (pat,color);
  78.     setfillstyle (12,color);
  79.     fillpoly(ncorners,vertices);
  80. }
  81.